var searchData = {};
+function ($) {
    'use strict';
    $.sidenav_width = function(){//页面进行缩放，左侧边栏也随之缩放
    	if($(document.body).width()<=970 && $('.toggle-sidebar').attr('screenWidth') == 'maxw'){ //展开状态
				$('.sidebar').css('width','4.4rem').addClass("nav-xs");
	   			$('.main-content').css('marginLeft','4.4rem')
	   			$('.toggle-sidebar').attr('toggleSidebar','shrink');
	   			$('.toggle-sidebar').attr('screenWidth','minw');
				
			}else if($(document.body).width()>=970 && $('.toggle-sidebar').attr('screenWidth') == 'minw'){
				$('.sidebar').css('width','14.4rem').removeClass("nav-xs")
		   		$('.main-content').css('marginLeft','14.4rem')
		   		$('.toggle-sidebar').attr('toggleSidebar','stretch')
		   		$('.toggle-sidebar').attr('screenWidth','maxw');
			}
    };
    $.button_submit = function($box){
        var $pageFooter=$box.find('.pageFooter'),$form=$box.find('form:first');
        $pageFooter.find(':submit').on('click',function(e){
            $form.submit();
        })
    }
    $.prototype.getFormData = function (argument) {
        var params = {}; 
       $.each($(this).serializeArray(),function(i,field){
            if(null!= field.value && ""!=field.value){
                params[field.name] = field.value;
            }
       });
        return params;
    }

    $.fn.extend({
    	ajaxUrl:function(op){
             
            var $this = $(this);
    		$.ajax({
    			type : op.type || 'GET',
    			url  : op.url,
    			data : op.data || {},
    			cache : false,//禁止ajax get方式从缓存中取数据
    			dataType :'html',//返回纯文本 HTML 信息；包含的 script 标签会在插入 dom 时执行
    			success : function(res){
                    // debugger
                     if (res.length==9371) { //当跳转到登录页面
                       location.href ="/login";
                     }else{
                        history.pushState({'url':op.url,'data':op.data}, '');
                        if(op.data){
                           if(op.data.search){
                            // console.log("在订单页面搜索之后进入了其中一个页面,将搜索值存起来");
                            searchData = op.data.search;
                           }
                         } 
                     $this.html(res);
                     }
    			},
    			error : function (xhr, ajaxOptions, thrownError){

    			},
    			statusCode : {
                    // 0  : function(xhr, ajaxOptions, thrownError) {
                    // },
                    503: function(xhr, ajaxOptions, thrownError) {
                        location.href="/severError";
                    }
                }
    		})
        },
        historyAjax:function(op){
      //没传数据get传数据post
            var $this = $(this);
            $.ajax({
    			type : op.data ==undefined ? 'get':'post',
    			url  : op.url,
    			data : op.data || {},
    			cache : false,//禁止ajax get方式从缓存中取数据
    			dataType :'html',//返回纯文本 HTML 信息；包含的 script 标签会在插入 dom 时执行
    			success : function(res){
                    // debugger
                      $this.html(res); //识别是订单页面
                     if(op.data){   //区分下可以不为data
                        $this.addClass("searchTag");
                      }
    			},
    			error : function (xhr, ajaxOptions, thrownError){

    			},
    			statusCode : {
                    // 0  : function(xhr, ajaxOptions, thrownError) {
                    // },
                    503: function(xhr, ajaxOptions, thrownError) {
                        location.href="/severError";
                    }
                }
    		})
        },
        formAjax:function(op){
            var $this = $(this);
            // debugger
            $.ajax({
    			type : op.type == undefined ? 'post':'get',
                url  : op.url,
                data : op.data || {},
                success: function (result) { 
                    if(result.length===9371){   //session超时返回的登录页面 避免接口返回的也是字符串不是json
                                location.href="/severError";
                        }else{  //正常处理
                            // var successStr = JSON.parse(result);
                            if(result.statusCode=="200"){
                                $this.modal('hide');
                                toastr.success(result.message+'!');
                                $(op.tableRefresh).bootstrapTable('refresh');
                            }else{           //订单重号    300           
                                toastr.warning(result.message+'!'); 
                            }
                        }
                  },  
                  error: function (result) {       //后台数据异常返回503
                    if(result.status===503||result.status===500){ 
                        $this.modal('hide');
                        toastr.error("服务端数据异常"+'!');
                    }
                  }  
    		})
        }
    });
     $.fn.extend({
     	loadMenu:function(op){
            var $this = $(this);
    		$.ajax({
    			type : op.type,
                url  : op.url,
                data :op.data,
    			dataType :'json',
    			success : function(json){
    				if(json){
                        $.each(json,function(i,item){
                            item.controllerUrl === '/order/**' ? orderBool = true : '';
                            if(item.children){
                                var  liContent='';   //填充的二级菜单
                                $.each(item.children,function(j,list){
                                    liContent +='<li class="nav-item"><a class="nav-link" href="'+list.url+'">'+list.name+'</a></li>'
                                })
                                $this.append(
                                    '<li class="nav-item">'+
                                        '<a class="nav-link"><i class="fa '+item.icon+'"></i><span class="menu-name">'+item.name+'</span> <span class="fa fa-caret-right fr" aria-hidden="true"></span></a>'+
                                        '<ul class="navbar-nav flex nav-second-level">'+
                                            liContent+
                                        '</ul>'+
                                    '</li>'
                                );
                            }else{                  //只有一级菜单的情况
                                $this.append(
                                    '<li class="nav-item">'+
                                      '<a class="nav-link" href="'+item.url+'"><i class="fa '+item.icon+'" aria-hidden="true"></i><span class="menu-name">'+item.name+'</span></a>'+
                                    '</li>'
                                );
                            }
                        })
                        
                    }
                   
    			}
                ,
    			error : function (xhr, ajaxOptions, thrownError){

    			}
    		})
    	}
     });
}(jQuery);

+function ($) {
    'use strict';
    var $main=$('.content-container');
    toastr.options = {  
        closeButton: false,  
        debug: false,  
        progressBar: true,  
        positionClass: "toast-top-center",  
        onclick: null,  
        showDuration: "300",  
        hideDuration: "1000",  
        timeOut: "2000",  
        extendedTimeOut: "1000",  
        showEasing: "swing",  
        hideEasing: "linear",  
        showMethod: "fadeIn",  
        hideMethod: "fadeOut"  
    };
    $('.nav-first-level').loadMenu({type:'get',url:'/menu'});
    $('.content-container').ajaxUrl({'url':'/main'});
    $('.nav-first-level').on('click','a',function(e){   //主要处理的是li标签元素
        var $this = $(this);
        var url = $this.attr('href');
        if($('.toggle-sidebar').attr('togglesidebar') =='shrink'){  //缩小状态 
            if(url && url != '#'){
                if($this.parents('.nav-second-level').length > 0){  //存在当前兄弟菜单栏的头
                    var firstLevelA=$this.parents('.nav-second-level').prev('a'); //父级菜单栏
                    firstLevelA.parent().addClass('open');              //父级菜单栏的一栏的头添加样式
                    firstLevelA.find('span.fa-caret-right').removeClass('fa-caret-right').addClass('fa-caret-down'); //当前父级菜单栏点开箭头朝下变化
                    firstLevelA.parent().siblings('li.open').find('.nav-second-level').css('display','none');
                    firstLevelA.parent().siblings('li.open').removeClass('open').find('span.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-right');
                    firstLevelA.next('.nav-second-level').css('display','block');
                }else{
                    $(this).parent('li').addClass('open');
                    $(this).parent('li').siblings('li.open').find('span.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-right');
                    $(this).parent('li').siblings('li.open').find('.nav-second-level').css('display','none');
                    $(this).parent('li').siblings('li.open').removeClass('open');
                }

            }else{
                event.preventDefault();
            }
        }else{

            if(!url || $(this).parents('.nav-second-level').length==0){

                var caretRight = $(this).find('span.fa-caret-right');  //找到右边指示箭头的方向 向右没打开二级菜单
                var caretDown = $(this).find('span.fa-caret-down');    //找到右边指示箭头的方向 向下打开二级菜单
                if(caretRight.length == 1){//收缩(除本身外存在二级菜单) 
                    $(this).parent().addClass('open');
                    $(this).find('span.fa-caret-right').removeClass('fa-caret-right').addClass('fa-caret-down');
                    $(this).parent().siblings('li.open').find('span.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-right');
                    $(this).parent().siblings('li.open').removeClass('open').children(".nav-second-level").slideUp(300);

                }else if(caretDown.length == 1){//展开(除本身外存在二级菜单)
                    $(this).parent().removeClass('open');
                    $(this).find('span.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-right');
                    
                }else{//收缩
                    $(this).parent().addClass('open');
                    $(this).parent().siblings('li.open').find('span.fa-caret-down').removeClass('fa-caret-down').addClass('fa-caret-right');
                    $(this).parent().siblings('li.open').removeClass('open').children(".nav-second-level").slideUp(300);
                }
                $(this).parent().children(".nav-second-level").slideToggle(300);
            }
            
        }
        if(url && url !='#'){   //点击的是当前的子菜单
            var thisParents=$this.parents('.nav-second-level');  //二级菜单栏存在1
            if(thisParents.length>0){
                $('.nav-second-level .nav-item.active').removeClass('active');//清场互相点击时其他原有点击状态
                $this.parent('li').addClass('active');//添加本菜单状态
                $('.breadcrumb').replaceWith('<ol class="breadcrumb"><li class="breadcrumb-item">'+thisParents.prev().find('.menu-name').html()+'</li><li class="breadcrumb-item open" aria-current="page"><a href="'+url+'">'+$this.html()+'</a></li></ol>');//标签整体替换
            }else{
                $('.nav-second-level .nav-item.active').removeClass('active');
                $('.breadcrumb').replaceWith('<ol class="breadcrumb"><li class="breadcrumb-item open" aria-current="page"><a href="'+url+'">'+$this.find('.menu-name').html()+'</a></li></ol>')
            }
            e.preventDefault();
            $('.content-container').ajaxUrl({'url':url}); //动态刷新主页面
        }
    });

    $('.crumbs').on("click","a",function(e){//面包屑导航单击事件
        var $this = $(this);
        e.preventDefault();
        var url = $this.attr('href');
        if(url=="/order/list"){   //订单菜单下三级目录存在，点击二级目录，删除之后的动态三级目录
           $this.parent().next().remove(); 
        }
        $main.ajaxUrl({'url':url});
    })
    $('.toggle-sidebar').click(function(){//菜单栏伸缩
        if($(this).attr('toggleSidebar') == 'stretch'){
            $('.sidebar').css('width','4.4rem').addClass("nav-xs");
            $('.main-content').css('marginLeft','4.4rem')
            $(this).attr('toggleSidebar','shrink');

        }else if($(this).attr('toggleSidebar') == 'shrink'){
            $('.sidebar').css('width','14.4rem').removeClass("nav-xs")
            $('.main-content').css('marginLeft','14.4rem')
            $(this).attr('toggleSidebar','stretch');
        }
    }) 
    $('.sidebar .logo a').click(function(e){
        var $this = $(this);
        var url = $this.attr('href');
        e.preventDefault();    //此处禁止点击后执行a的默认条件直接跳转
        $main.ajaxUrl({'url':url});
    })
    window.onpopstate = function (event) {  //浏览器回退加载的是ajax存储的
    //    debugger
          if(searchData!=""&&event.state.url==="/order/list"){
            //    console.log("回退有数据说明执行过搜索，在加载首页时候再执行表格刷新"); //将数据传入进去
               event.state.data = searchData;
               $('.content-container').historyAjax(event.state);
          }else{ //正常回退除了订单页面的回退
            $('.content-container').historyAjax(event.state);
          }
    };
}(jQuery);


+(function ($) {//匿名函数也是函数表达式也是闭包也是自执行函数，这整个函数表达式就是返回值，可以访问外部变量，外部环境无法范围内部变量

    window.Ewin = function () {//匿名函数，Ewin代表这个函数的返回值
        var html = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +
                           '<div class="modal-dialog modal-sm">' +
                             '<div class="modal-content">' +
                               '<div class="modal-header">' +
                               '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
                                 '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +
                               '</div>' +
                               '<div class="modal-body">' +
                               '<p>[Message]</p>' +
                               '</div>' +
                                '<div class="modal-footer">' +
                                '<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>' +
                                '<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>' +
                              '</div>' +
                             '</div>' +
                           '</div>' +
                         '</div>';
        var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');  
        var generateId = function () {
                        var date = new Date();
                        return 'mdl' + date.valueOf();
                    };
        var init = function (options) {
                        options = $.extend({}, {
                            title: "操作提示",
                            message: "提示内容",
                            btnok: "确定",
                            btncl: "取消",
                            width: 200,
                            auto: false
                        }, options || {});
                        var modalId = generateId();
                        var content = html.replace(reg, function (node, key) {
                            return {
                                Id: modalId,
                                Title: options.title,
                                Message: options.message,
                                BtnOk: options.btnok,
                                BtnCancel: options.btncl
                            }[key];
                        });
                       $('body').append(content);
                        $('#' + modalId).modal({
                            width: options.width,
                            backdrop: 'static'
                        });
                        $('#' + modalId).on('hide.bs.modal', function (e) {
                            $('body').find('#' + modalId).remove();
                        });
                        return modalId;
                    }
        return {
           
            dialog: function (options) {
                var dialogdHtml = '<div class="modal fade" id="[Id]" tabindex="-1" role="dialog" aria-labelledby="[Label]" aria-hidden="true">'+
                      '<div class="modal-dialog modal-dialog-centered" role="document" style="[Style]">'+
                        '<div class="modal-content">'+
                          '<div class="modal-header">'+
                            '<h5 class="modal-title" id="[lableId]">[Title]</h5>'+
                            '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'+
                              '<span aria-hidden="true">&times;</span>'+
                            '</button>'+
                          '</div>'+
                            '[Content]'+
                        '</div>'+
                      '</div>'+
                    '</div>'
               

                options = $.extend({}, {
                    id:'dialog1',
                    title: 'dialog1',
                    url: '',
                    data:{},
                    backdrop:'static',
                    width: 500,
                    height: 400,
                    onReady: function (e) {
                        var $box = $(e.target);
                        $.button_submit($box);
                    },
                }, options || {});
                
                $.post(options.url,options.data,function(data,status){
                    if(status == 'success'){
                        var content = dialogdHtml.replace(reg, function (node, key) {//匹配每个参数对dialogdHtml进行正则匹配。
                            return {
                                Id: options.id,
                                Title: options.title,
                                Label: options.id+'Label',
                                lableId:options.id+'Label',
                                Content:data,                         //路由返回的页面作为data。
                                Style:'max-width:'+options.width+'px;',
                            }[key];
                        });
                        $('body').append(content);
                        var target = $('#' + options.id);
                        target.find('.modal-body').css('height',options.height+'px')
                        $('#'+options.id).modal({
                            width: options.width,
                            height:options.height,
                            backdrop: options.backdrop     // static点击背景空白处不被关闭
                        });    
                        target.on('hide.bs.modal', function (e) {   //此事件在模态框被隐藏（并且同时在 CSS 过渡效果完成）之后被触发
                            $('body').find(target).remove();          //移除模态
                        });
                        target.on('shown.bs.modal', function (e) {  //模态框已经显示出来（并且同时在 CSS 过渡效果完成）之后被触发
                            if (options.onReady(e))
                                options.onReady.call(target, e);   //target调用option的onReady方法
                        });
                    }

                });
                
            },
            confirm: function (options) {
                                var id = init(options);
                                var modal = $('#' + id);
                                modal.find('.ok').removeClass('btn-primary').addClass('btn-success');
                                modal.find('.cancel').show();
                                return {
                                    id: id,
                                    on: function (callback) {
                                        if (callback && callback instanceof Function) {
                                            modal.find('.ok').click(function () { callback(true); });
                                            modal.find('.cancel').click(function () { callback(false); });
                                        }
                                    },
                                    hide: function (callback) {
                                        if (callback && callback instanceof Function) {
                                            modal.on('hide.bs.modal', function (e) {
                                                callback(e);
                                            });
                                        }
                                    }
                                };
                            },
        }
    }();
})(jQuery);

function bsConfirm(info,order_edit_controlConform,num,callback){//规定只处理num为1
    Ewin.confirm({ message: info }).on(function (e) {
           if (!e) { //点击否就关闭当前页面
                return;
            }else{    //确定之后执行执行开始操作
                order_edit_controlConform(num)
            }
                callback
    });
}
